package com.yyfax.pay.depository.controller.api;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.common.Log;
import com.yyfax.framework.common.OpResponse;
import com.yyfax.pay.common.annotation.CheckSign;
import com.yyfax.pay.common.enums.FuiouDepositStateEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.common.model.to.YYPayApiReq2;
import com.yyfax.pay.depository.service.inner.FuiouUserService;
import com.yyfax.pay.depository.vo.FuiouUserProtocolVO;
import com.yyfax.pay.vo.depository.QueryUserProtocolSignReq;
import com.yyfax.pay.vo.depository.UserProtocolSignSmsReq;
import com.yyfax.pay.vo.depository.UserProtocolSigningReq;

/**
 * 富友存管用户控制类
 * 
 * @author buyi
 * @date 2018-06-13 16:30:55
 * @since v1.0.0
 */
@RestController
public class UserApiController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private FuiouUserService fuiouUserService;

	/**
	 * 协议绑定发送短信验证码
	 * 
	 * @author buyi
	 * @date 2018-06-14 15:50:46
	 * @since v2.2.2
	 * @param req
	 * @return
	 */
	@CheckSign
	@PostMapping("/api/fuiou/user/protocol/sign_sms")
	public OpResponse doProtocolSignSms(@RequestBody @Valid YYPayApiReq2<UserProtocolSignSmsReq> req) {
		// 保存订单信息
		FuiouUserProtocolVO vo = fuiouUserService.saveProtocol(req.getMerchantCode(), req.getParams());

		if (!FuiouDepositStateEnum.INI.isValue(vo.getFuiouUserProtocol().getState())) {
			// 如果订单不是初始化状态，则直接返回已经操作的响应码
			logger.warn(Log.op("UserApiController.doProtocolSignSms").msg("协议绑定获取短信失败，订单状态不是初始化状态，无法获取短信")
					.kv("busiId", vo.getFuiouUserProtocol().getBusiId()).kv("state", vo.getFuiouUserProtocol().getState()).toString());

			return OpResponse.fail(PayExceptionCode.ERROR_OPT_FAIL.getCode(), "获取短信失败，操作已过期", null);
		}

		// 短信冷却控制
		fuiouUserService.doProtocolSignSmsCooldown(vo.getFuiouUserProtocol());

		// 触发发送短信
		return fuiouUserService.doProtocolSignSmsComposite(vo.getFuiouUserProtocol());
	}

	/**
	 * 协议确认
	 * 
	 * @author buyi
	 * @date 2018-06-19 20:04:58
	 * @since v2.2.2
	 * @param req
	 * @return
	 */
	@CheckSign
	@PostMapping("/api/fuiou/user/protocol/signing")
	public OpResponse doProtocolSigning(@RequestBody @Valid YYPayApiReq2<UserProtocolSigningReq> req) {
		return fuiouUserService.doProtocolSigningComposite(req.getMerchantCode(), req.getParams());
	}

	/**
	 * 用户协议签约查询
	 * 
	 * @author buyi
	 * @date 2018-06-27 14:00:50
	 * @since v2.2.2
	 * @param req
	 * @return
	 */
	@CheckSign
	@PostMapping("/api/fuiou/user/protocol/query")
	public OpResponse queryProtocolSign(@RequestBody @Valid YYPayApiReq2<QueryUserProtocolSignReq> req) {
		//校验可选参数
		req.getParams().valid();
		
		return fuiouUserService.queryProtocolSignComposite(req.getMerchantCode(), req.getParams());
	}
}
